package com.snake.win.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.snake.win.GameWin;
import com.snake.win.obj.Snake;
import com.snake.win.obj.BodyObj;
import com.snake.win.obj.FoodObj;
import com.snake.win.obj.GameObj;
import com.snake.win.obj.HeadObj;
import com.snake.win.utils.GameUtils;

import java.util.ArrayList;
import java.util.HashSet;

public class GameScreen implements Screen {
    final GameWin game;

    static Snake snake;

    public BitmapFont font;
    ShapeRenderer shapeRenderer;

    public enum State {
        Init, RUNNING, PAUSE, DIE, FINISHED, RETRY, NEXT
    }

    public State state;

    private volatile boolean isRunning = true;

    public GameScreen(final GameWin game) {
        this.game = game;

        snake = new Snake();
        snake.headObj = new HeadObj(GameUtils.rightImg, 4, 1);
        snake.bodyObjList = new ArrayList<>();
        snake.foodObj = new FoodObj().getFood(snake);
        snake.tailobj = new GameObj(1, 1);

        state = State.Init;

        snake.setMap(new HashSet<>());

        shapeRenderer = new ShapeRenderer();
        game.viewport = new FitViewport(30, 22);
        font = new BitmapFont();

        snake.bodyObjList.add(new BodyObj(GameUtils.bodyImg, 3, 1));
        snake.bodyObjList.add(new BodyObj(GameUtils.bodyImg, 2, 1));
        snake.updataMap();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRunning) {
                    Snake.Direction aidirection = snake.snakeAi.play2(snake);
                    if(snake.canAI) {
                        Gdx.app.postRunnable(new Runnable() {
                            @Override
                            public void run() {
                                snake.AIDirection = aidirection;
                            }
                        });
                    }
                }
            }
        }).start();
    }

        @Override
        public void show () {

        }

        @Override
        public void render ( float v){
            input();
            logic();
            snake.ChangeDirection();
            snake.update(game.batch);
            draw();
//        try {
//            Thread.sleep(30);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        }

        private void input () {
            if (Gdx.input.isKeyJustPressed(Input.Keys.X)) {
                game.server.start();
            }
            if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
                game.client.connect();
            }
            if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
                switch (state) {
                    case Init:
                        state = State.RUNNING;
                        break;
                    case RUNNING:
                        state = State.PAUSE;
                        break;
                    case PAUSE:
                        state = State.RUNNING;
                        break;
                    case DIE:
                        state = State.RETRY;
                        break;
                    case FINISHED:
                        state = State.NEXT;
                        break;
                    default:
                        break;

                }
            }
        }


        private void logic () {

        }

        private void draw () {
            ScreenUtils.clear(Color.GRAY);
            game.viewport.apply();
            game.batch.setProjectionMatrix(game.viewport.getCamera().combined);
            shapeRenderer.setProjectionMatrix(game.viewport.getCamera().combined);

            // 绘制蛇身、蛇头和食物
            game.batch.begin();
            for (GameObj obj : snake.bodyObjList) {
                obj.paintSelf(game.batch);
            }
            snake.foodObj.paintSelf(game.batch);
            snake.headObj.paintSelf(game.batch);
            game.batch.end();

        /*// 绘制记分面板背景
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(Color.DARK_GRAY);
        shapeRenderer.rect(22, 0, 8, 22);
        shapeRenderer.end();*/

        /*// 绘制分数
        spriteBatch.begin();
        String scoreText = "Score: " + objManager.score;
        GameUtils.drawWord(spriteBatch, scoreText, 23, 20);
        spriteBatch.end();*/

            // 绘制网格线
            shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
            for (int i = 0; i <= 21; i++) {
                shapeRenderer.line(1, i, 21, i);
                shapeRenderer.line(i, 1, i, 21);
            }
            shapeRenderer.end();
        }

        @Override
        public void resize ( int width, int height){
            game.viewport.update(width, height, true); // true centers the camera
        }

        @Override
        public void pause () {

        }

        @Override
        public void resume () {

        }

        @Override
        public void hide () {

        }

        @Override
        public void dispose () {
            isRunning=false;
            //其他释放资源代码
        }
    }
